/*
 MIT License
 
 Copyright (c) 2021 Steven Le

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

/**
 *	lx_more.c - version 0.2 of lxmore
 *	
 *	read and print 24 lines then pause for a few special commands
 *      feature of version 0.2: reads from /dev/tty for commands
 */

#include <stdio.h>
#include <stdlib.h>

#define PAGELEN 24
#define LINELEN 512

void do_more(FILE *);
int see_more(FILE *);

int main(int ac, char *av[])
{
        // 声明文件类型变量
        FILE *fp;
        if (ac == 1) {
                // 将输出
                do_more(stdin);
        } else {
                while(--ac) {
                        if ((fp = fopen(*++av, "r")) != NULL) {
                                do_more(fp);
                                fclose(fp);
                        } else
                                exit(1);
                }
	}
        return 0; 		
}

/**
 *      read PAGELEN lines, then call see_more() for further more
 */
void do_more( FILE *fp)
{
       char line[LINELEN];
       int num_of_lines = 0;

        // create a tty object to create pipe 
       FILE *fp_tty;
       fp_tty = fopen("/dev/tty", "r");         /* NEW: cmd stream */

       int see_more(FILE *), reply;
       while (fgets(line, LINELEN, fp)) {       /* more input */ 
                if (num_of_lines == PAGELEN) {  /* full screen? */ 
                        reply = see_more(fp_tty);     /* y: ask user */ 
                        if (reply == 0)         /* n: done */ 
                                break;
                        num_of_lines -= reply;  /* reset count */ 
                }
                if (fputs(line, stdout) == EOF) /* show lines */
                        exit(1);                /* or die */
                num_of_lines ++;                /* count it */ 
        
       }
                
}

int see_more(FILE * cmd)
/** 
 *      print message, wait for response, return # of lines 
 *      q means no, space means yes ,CR means one line
 */ 
{
        int c;
        printf("\033[7m more? \033[m");        /* reverse on a vt100 */
        while((c = getc(cmd)) != EOF)           /* get response */
        {
                if (c == 'q')                   /* q => N */
                        return 0;
                if (c == ' ')                   /* ' ' => next page */
                        return PAGELEN;
                if (c == '\n')                  /* Enter key => 1 line */
                        return 1;
        }
        return 0;
}
